<template>
  <div>
    <HeadOptions></HeadOptions>
    <!-- <Float :profit="profit" :vid="vid" v-if="freeorno ==0"></Float> -->
    <div class="boominfo">
      <img :src="pic" alt>
      <span class="boominfo_title">{{title}}</span>
      <span class="boominfo_txtandoption">
        <span class="boominfo_txt">{{detail}}</span>
        <Options :collection="collection" :vid="vid"></Options>
      </span>
      <span class="boominfo_tip">
        <p>已更新{{thenew}}期 |</p>
        <p>{{buyer}}人订阅</p>
      </span>
    </div>
    <van-tabs
      v-model="active"
      color="#187a72"
      title-active-color="#187a72"
      title-inactive-color="black"
      style="margin-bottom: 60px"
    >
      <van-tab title="详情">
        <div class="xiangqing" v-html="fuwenben"></div>
      </van-tab>
      <van-tab title="目录">
        <span class="mulu">
          <p>内容</p>
        </span>
        <VedioList :vediolist="vediolist"></VedioList>
      </van-tab>
    </van-tabs>
    <BottomBtn
      :yuanjia="xianjia"
      :xianjia="yuanjia"
      v-on:listersonpay="parentpay"
      v-if="buyorno == 1 || freeorno ==0"
    >
      <template #txt>订阅专栏</template>
    </BottomBtn>
  </div>
</template>

<script>
  import BottomBtn from './components/BottomBtn'
  import VedioList from './components/VedioList'
  import Options from './components/Options'
  import HeadOptions from './components/HeadOptions'
  import Float from './../DetailOfVedio/components/Float'

  import * as fetch from '@/api/fetch'

  import Vue from 'vue'

  import { Tab, Tabs } from 'vant'
  Vue.use(Tab).use(Tabs)

  import { Toast } from 'vant'
  Vue.use(Toast)

  import { weixinConfig, chooseWXPay } from '@/api/wechatPay'

  export default {
    name: 'DetailOfBoom',
    data() {
      return {
        active: 2,
        left: 200,
        top: 200,
        timer: null,
        currentTop: 0,

        vid: '',
        pic: '',
        title: '',
        detail: '',
        fuwenben: '',
        vediolist: [],
        collection: '',
        yuanjia: '',
        xianjia: '',
        thenew: '',
        buyer: '',

        buyorno: '',
        freeorno: '',

        inviter: 0,
        spread_id: 0,
      }
    },
    components: {
      BottomBtn,
      VedioList,
      Options,
      HeadOptions,
      Float,
    },
    methods: {
      parentpay() {
        this.processOrder().then(res => {
          if (res.errcode == 0) {
            weixinConfig(res.data.signPackage)

            chooseWXPay(res.data, {
              success: function(res) {
                Toast.success('支付成功')
                window.location.reload()
              },
              cancel() {
                alert('已取消支付')
              },
              fail() {
                alert('购买失败，请重新创建订单')
              },
            })
          }
        })
      },

      async getVideoInfo() {
        const res = await fetch.getVideoInfo({
          vid: this.$route.query.vid,
          page: 1,
        })
        console.log(res)
        if (res.errcode == 0) {
          this.vid = res.data.vid
          this.profit = res.data.profit
          this.pic = res.data.picture
          this.title = res.data.title
          this.detail = res.data.detail
          this.fuwenben = res.data.content
          this.vediolist = res.data.table
          this.collection = res.data.collection
          this.yuanjia = res.data.market_price
          this.xianjia = res.data.shop_price
          this.thenew = res.data.new
          this.buyer = res.data.buyer
          this.buyorno = res.data.is_buy
          this.freeorno = res.data.is_free
        }
      },

      async saveUserInvitation() {
        const res = await fetch.saveUserInvitation({
          vid: this.$route.query.vid,
          inviter: this.inviter,
        })
        console.log(res)
        return res
      },

      async playVideoLog() {
        const res = await fetch.playVideoLog({
          vid: this.$route.query.vid,
          length: 0,
        })
        console.log(res)
      },

      async processOrder() {
        console.log('开始支付')
        console.log(this.inviter)
        console.log(this.spread_id)

        const res = await fetch.processOrder({
          vid: this.$route.query.vid,
          inviter: this.inviter,
          spread_id: this.spread_id,
        })
        console.log(res)
        return res
      },
    },
    async processOrder() {
      console.log('开始支付')
      console.log(this.inviter)
      console.log(this.spread_id)

      const res = await fetch.processOrder({
        vid: this.$route.query.vid,
        inviter: this.inviter,
        spread_id: this.spread_id,
      })
      console.log(res)
      return res
    },
    created() {},
    mounted() {
      this.vid = this.$route.query.vid
    },
    activated() {
      this.getVideoInfo()
      this.playVideoLog()

      console.log(this.$route.query.vid)
      console.log(this.$route.query.inviter)
      console.log(this.$route.query.spread_id)
      if (
        this.$route.query.inviter == undefined &&
        this.$route.query.spread_id == undefined
      ) {
        this.inviter = 0
        this.spread_id = 0
      } else {
        this.inviter = this.$route.query.inviter
        this.spread_id = this.$route.query.spread_id
        this.saveUserInvitation()
      }
    },
  }
</script>
<style>
</style>
<style lang="less" scoped>
  /deep/ .xiangqing {
    section {
      max-width: 100%;
      box-sizing: border-box;
    }
    img {
      max-width: 100%;
    }
  }
  .boominfo {
    padding-bottom: 10px;
    border-bottom: 10px solid #f5efef;
    img {
      margin-top: 40px;
      width: 100%;
      height: 300px;
    }
    .boominfo_title {
      display: block;
      margin: 10px 15px;
      line-height: 20px;
      font-size: 15px;
    }
    .boominfo_txtandoption {
      display: flex;
      margin: 10px 15px;
      flex-direction: row;
      .boominfo_txt {
        font-size: 12px;
        line-height: 19px;
        flex-grow: 1;
        overflow: hidden;
        text-overflow: ellipsis;
        display: -webkit-box;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
        width: 60%;
      }
    }
    .boominfo_tip {
      display: flex;
      flex-direction: row;
      margin-left: 15px;
      font-size: 12px;
      color: #acacac;
      p:last-child {
        margin-left: 5px;
      }
    }
  }
  .xiangqing {
    margin: 15px 0px 60px 0;
  }
  .mulu {
    display: flex;
    justify-content: space-between;
    margin: 15px 15px;
    p:last-child {
      font-size: 13px;
      color: #acacac;
    }
  }
</style>
